package kr.hyosang.android.penfo;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedObject
{
    private static SharedObject sharedObject;
    
    private static final String KEY_PHPSESSID = "PHPSESSID";
    private static final String KEY_ZBSESSIONID = "ZBSESSIONID";
    
    
    public Context mAppContext;
    public float fDensity;
    public int nListItemHeight;
    public boolean bLogin;
    private String cookiePHPSESSID;
    private String cookieZBSESSIONID;
    private SharedPreferences mPref;
    
    public static SharedObject getInstance() {
        if(sharedObject == null) {
            sharedObject = new SharedObject();
        }
        
        return sharedObject;
    }
    
    public void init(Context ctx) {
        mAppContext = ctx.getApplicationContext();
        fDensity = ctx.getResources().getDisplayMetrics().density;
        nListItemHeight = getDip2Pixel(ctx.getResources().getString(R.string.article_list_height));
        mPref = mAppContext.getSharedPreferences("auto-login", Activity.MODE_PRIVATE);
        bLogin = false;
        
        this.cookiePHPSESSID = mPref.getString(KEY_PHPSESSID, "");
        this.cookieZBSESSIONID = mPref.getString(KEY_ZBSESSIONID, "");
    }
    
    public int getDip2Pixel(String dip) {
    	dip = dip.replace("dip", "");
    	
    	float d = Float.parseFloat(dip);
    	return (int)(d * fDensity);
    }
    
    private void setPreference(String key, String val) {
        Editor editor = mPref.edit();
        editor.putString(key, val);
        editor.commit();
    }
    
    public void setPHPSESSID(String sessid) {
        cookiePHPSESSID = sessid;
        setPreference(KEY_PHPSESSID, sessid);
    }
    
    public void setZBSESSIONID(String zbsessid) {
        if("deleted".equals(zbsessid)) {
            bLogin = false;
            zbsessid = "";
        }
        
        cookieZBSESSIONID = zbsessid;
        setPreference(KEY_ZBSESSIONID, zbsessid);
    }
    
    public void logout() {
        setZBSESSIONID("");
        bLogin = false;
    }
    
    public String getPHPSESSID() {
        return cookiePHPSESSID;
    }
    
    public String getZBSESSIONID() {
        return cookieZBSESSIONID;
    }
    
    public void setCookies(List<String> cookies) {
        if(cookies == null) return;
        for(String c : cookies) {
            String [] tokens = c.split(";");
            
            for(String t : tokens) {
                if(t.startsWith("PHPSESSID=")) {
                    this.setPHPSESSID(t.substring(10));
                    break;
                }else if(t.startsWith("ZBSESSIONID=")) {
                    this.setZBSESSIONID(t.substring(12));
                    break;
                }
                
                break;
            }
        }
    }
}
